import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { ChatService } from './chat.service';
import { SendMessageDto } from './dto/send-message.dto';
import { GetMessagesDto } from './dto/get-messages.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';

@Controller('chat')
@UseGuards(JwtAuthGuard)
export class ChatController {
  constructor(private chatService: ChatService) {}

  @Post(':chatRoomId/messages')
  async sendMessage(
    @Param('chatRoomId') chatRoomId: string,
    @Request() req,
    @Body() sendMessageDto: SendMessageDto,
  ) {
    return this.chatService.sendMessage(
      parseInt(chatRoomId),
      req.user.id,
      sendMessageDto,
    );
  }

  @Get(':chatRoomId/messages')
  async getMessages(
    @Param('chatRoomId') chatRoomId: string,
    @Request() req,
    @Query() getMessagesDto: GetMessagesDto,
  ) {
    return this.chatService.getMessages(
      parseInt(chatRoomId),
      req.user.id,
      getMessagesDto,
    );
  }

  @Put('messages/:messageId')
  async editMessage(
    @Param('messageId') messageId: string,
    @Request() req,
    @Body('content') content: string,
  ) {
    return this.chatService.editMessage(
      parseInt(messageId),
      req.user.id,
      content,
    );
  }

  @Delete('messages/:messageId')
  async deleteMessage(@Param('messageId') messageId: string, @Request() req) {
    return this.chatService.deleteMessage(parseInt(messageId), req.user.id);
  }
}
